# Maintainer: Jordan Irwin <antumdeluge@gmail.com>

_realname=md4c
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgbase="mingw-w64-${_realname}"
pkgver=0.3.4
pkgrel=2
pkgdesc="C Markdown parser implementation compliant to CommonMark specification"
arch=("any")
url="https://github.com/mity/md4c"
license=("MIT")
depends=()
makedepends=("bsdtar"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gcc")
source=("${url}/archive/release-${pkgver}.tar.gz")
sha256sums=("cf3f6b75d4b1304551a68272d3b403e4cd0952d976a280e8b7db68090354486f")
extractdir="${_realname}-release-${pkgver}"
builddir_static="build-${CARCH}-static"

prepare() {
    cd "${srcdir}/${extractdir}"
    patch -p1 -i "${startdir}/001-md4c-0.3.4-md2html-add-exe-suffix.patch"
}

build() {
    # TODO: add shared build
    # static build
    [[ -d "${srcdir}/${builddir_static}" ]] && rm -rf "${srcdir}/${builddir_static}"
    mkdir -p "${srcdir}/${builddir_static}" && cd "${srcdir}/${builddir_static}"

    declare -a extra_config
    if check_option "debug" "n"; then
        extra_config+=("-DCMAKE_BUILD_TYPE=Release")
    else
        extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
    fi

    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}/bin/cmake" \
        -G "Ninja" \
        -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
        -DBUILD_SHARED_LIBS=ON \
        "${extra_config[@]}" \
        "../${extractdir}"

    ninja
}

package() {
    cd "${srcdir}/${builddir_static}"

    DESTDIR="${pkgdir}" ninja install

    # license
    install -Dm644 "${srcdir}/${extractdir}/LICENSE.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.md"
}
